
package vn.aptech.project.model.dao;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import vn.aptech.project.model.dto.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class  orderline_DAO extends CommonDAO {
 public  orderline_DAO() throws ClassNotFoundException, SQLException, IOException {
 }


public Vector selectall() throws SQLException
{
String strSelect = "SELECT * FROM FS_oderline";
return ExecSelect(strSelect);
}
public Vector selectByOrderid(int orderid) throws SQLException
{
String strSelect = "SELECT FS_product.productid,FS_category.typeinfo,FS_product.productname,FS_orderline.price,FS_orderline.quantity FROM FS_orderline join FS_product on FS_orderline.productid=FS_product.productid inner join FS_category on FS_product.Typeid=FS_category.Typeid where FS_orderline.Orderid="+orderid;
return ExecSelect(strSelect);
}
public Vector selectByProductid(int productid) throws SQLException
{
String strSelect = "SELECT * FROM FS_orderline where productid="+productid;
return ExecSelect(strSelect);
}
public int getQuantityByOrderid(int orderid) throws SQLException, FileNotFoundException, IOException
{
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             } catch (ClassNotFoundException ex)
            {
            Logger.getLogger(tblorder_DAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=FurnitureSystem;user=sa;password=1234567";
            Connection con = DriverManager.getConnection(connectionString);
            Statement statement = con.createStatement();
            String str = "Select quantity from FS_order where orderid="+orderid;
            ResultSet rs = statement.executeQuery(str);
            rs.next();
            Vector vt = new Vector();
            vt.add(rs.getObject("quantity"));
            int a =Integer.parseInt(String.valueOf(vt.get(0)));
            return a;

}
public int insert(oderline_DTO  obj) throws SQLException
{
String strInsert = "INSERT INTO FS_orderline VALUES ("+obj.getOrderid() +" ,"+ obj.getproductid() +" ,"+obj.getprice()+" ,"+ obj.getquantity() +")";
return  ExecNonQuery(strInsert);
}


public int update(oderline_DTO  obj) throws SQLException
{
String strUpdate = "UPDATE FS_orderline SET productid="+obj.getproductid() +","+"quantity="+obj.getquantity() +","+"price="+obj.getprice() +"WHERE Orderid="+ obj.getOrderid() ;
return  ExecNonQuery(strUpdate);
}


public int delete(int Orderid) throws SQLException
{
String strDelete = "DELETE FROM FS_orderline WHERE Orderid="+Orderid;
return  ExecNonQuery(strDelete);
}
}

